﻿using DomainService.IService.Sys;
using DomainService.Service.Sys;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model.BusinessModel.OutPut;
using Model.BusinessModel.OutPut.sys;
using Model.DomainModel.Sys;
using Utility;
using Utility.Enum;

namespace OverallAuth_WebApi.Controllers.Sys
{
    /// <summary>
    /// 部门api
    /// </summary>
    [ApiController]
    [Route("api/[controller]/[action]")]
    [ApiExplorerSettings(GroupName = nameof(ModeuleGroupEnum.SysDepartment))]
    public class SysDepartmentController : BaseController
    {
        #region 构造实列化

        /// <summary>
        /// 部门服务
        /// </summary>
        public ISysDepartmentService _sysDepartmentService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sysDepartmentService"></param>
        public SysDepartmentController(ISysDepartmentService sysDepartmentService)
        {
            _sysDepartmentService = sysDepartmentService;
        }

        #endregion

        #region 接口

        /// <summary>
        ///  获取公司下的部门
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<TreeOutPut> GetCorporationDepartmentList(string? corporationKey)
        {
            ReceiveStatus<TreeOutPut> receiveStatus = new();
            if (string.IsNullOrWhiteSpace(corporationKey))
                corporationKey = GetLoginUserMsg().CorporationKey;
            var list = _sysDepartmentService.GetCorporationDepartmentList(corporationKey ?? string.Empty);
            receiveStatus.data = list;
            return receiveStatus;

        }

        /// <summary>
        /// 获取所有公司及部门--登录公司为星辰公司，可以获取全部，否则只获取登录公司数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<TreeOutPut> GetAllCorporationDepartmentList(string? corporationKey)
        {
            ReceiveStatus<TreeOutPut> receiveStatus = new();
            if (string.IsNullOrWhiteSpace(corporationKey))
                corporationKey = GetLoginUserMsg().CorporationKey;
            var list = _sysDepartmentService.GetAllCorporationDepartmentList(corporationKey ?? string.Empty);
            receiveStatus.data = list;
            return receiveStatus;
        }

        /// <summary>
        /// 新增机构
        /// </summary>
        /// <param name="sysDepartment">机构数据</param>
        [HttpPost]
        public ReceiveStatus Insert(SysDepartment sysDepartment)
        {
            sysDepartment.CreateTime = DateTime.Now;
            sysDepartment.CreateUser = GetLoginUserMsg().UserId;
            return _sysDepartmentService.Insert(sysDepartment);
        }

        /// <summary>
        /// 修改机构
        /// </summary>
        /// <param name="sysDepartment">机构数据</param>
        [HttpPost]
        public ReceiveStatus Update(SysDepartment sysDepartment)
        {
            sysDepartment.CorporationKey = GetLoginUserMsg().CorporationKey;
            return _sysDepartmentService.Update(sysDepartment);
        }

        /// <summary>
        /// 根据部门ID删除部门
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <returns></returns>
        [HttpDelete]
        public ReceiveStatus Delete(string id)
        {
            return _sysDepartmentService.Delete(id);
        }

        /// <summary>
        /// 根据部门ID获取部门人员
        /// </summary>
        /// <param name="id">部门ID</param>
        /// <param name="corporationKey">公司key</param>
        /// <returns></returns>
        [HttpGet]
        public ReceiveStatus<SysDepartmentOutPut> GetDepartmentUserList(string? id, string? corporationKey)
        {
            return _sysDepartmentService.GetDepartmentUserList(id, corporationKey);
        }
        #endregion
    }
}
